package com.wqh.eurekaconsumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author wanqh
 * @date 2018/01/07 14:38
 * @description:
 */
@RestController
public class DcController {
    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Autowired
    RestTemplate restTemplate;

    /**
     *
     * 消费eureka-client提供的服务
     * @return
     */
    @GetMapping("/consumer")
    public String dc(){
        //负载均衡的选出eureka-client服务实例
        ServiceInstance choose = loadBalancerClient.choose("eureka-client");
        //构建服务的url
        String url =  "http://"+choose.getHost()+":"+choose.getPort()+"/dc";
        System.out.println(url);
        //利用RestTemplate对象实现对服务提供者接口的调用
        return restTemplate.getForObject(url,String.class);
    }
}
